/* *
 * @program: VideoDirector
 *
 * @description: ${description}
 *
 * @author: 909845
 *
 * @create: 2019-03-12 20:07
***/

#include "FlvRemux.h"

#define TAG "TID[" << m_strTaskId << "]- "

CFlvRemux::CFlvRemux(const string &strTaskID) : m_strTaskId(strTaskID)
{
    DEBUG_LOG(TAG << "Create CFlvRemux");
}

CFlvRemux::~CFlvRemux()
{
    DEBUG_LOG(TAG << "Destroy ~CFlvRemux");
}

Bool CFlvRemux::Initlize(const FuncRemuxStreamCb remuxStreamCb, bool bHaveAudio)
{
    Bool bRet = False;
    do
    {
        Assert(remuxStreamCb != nullptr);
        m_remuxStreamCb = remuxStreamCb;
        m_packFlv.Initilize(this, bHaveAudio);
        bRet = True;
    } while (0);

    return bRet;
}

void CFlvRemux::OnFlvStream(const char *pData, const Int32 nLen)
{
    m_remuxStreamCb(pData, nLen);
}

void CFlvRemux::RecvVideoStream(const char *pData, int nLen, unsigned int uTimestamp)
{
    if (!m_bStartSps)
    {
        const EH264NalType type = CH264Tools::GetNalType((const UInt8 *) pData, nLen, CH264Tools::GetH264StartNum((const UInt8 *) pData, nLen));
        if (EH264NAL_SPS != type)
        {
            TRACE_LOG(TAG << "Wait for SPS.");
            return;
        }
        m_bStartSps = true;
    }

    if (m_bStartSps)
    {
        //const UInt32 uTimeStap = OS::Milliseconds() & 0xffffffff;
        m_packFlv.RecH264Stream((const UInt8 *) pData, nLen, uTimestamp);
    }
}

void CFlvRemux::RecvAudioStream(const char *pData, int nLen, unsigned int uTimestamp)
{
    if (m_bStartSps)
    {
        const UInt32 uTimeStap = OS::Milliseconds() & 0xffffffff;
        m_packFlv.RecAacStream((const UInt8 *) pData, nLen, uTimeStap);
    }
}

